<template>
  <view :style="mix_diyStyle">
  <!--  <home-deco
      :deco_info="deco_data"
      :width="width"
      :height="height"
      :home_page_img="home_page_img"
      :is_show_top="true"
      :home_is_show_top_cat="home_is_show_top_cat"
      :is_from_found="false"
      type="home"
      :overflow="privacyPolicyModel"
      @cartUpdate="getCartNum"
      ref="homeDeco"
      @needLogin="needLogin"
      :is_from_founds="true"
    >
    </home-deco> -->
<!-- 	<view class="" style="height: 120rpx;background-color: #fff;">
		
	</view> -->
	<view   class="" v-if="(authounum==1 ||authounum==2 ||authounum==5) && rezub">
		<homeIndex ></homeIndex>
	</view>
	<view class="" v-if="webinfo.roles==5">
		<homeAssociaton :webinfo="webinfo"></homeAssociaton>
	</view>
	<view   class="" v-if="webinfo.roles==2">
		<homeTourist
		:webinfo="webinfo"
		></homeTourist>
	</view>
	<!-- <view  v-if="!wenshow" >
		 
		<homeTourist
		:webinfo="webinfo"
		></homeTourist>
		
		
		
	</view> -->
	
    <!-- 登录弹框 -->
    <loginPop ref="loginPop" @confirmLogin="confirmLogin"></loginPop>
    <!-- 登录弹框 -->
	<!-- #ifdef MP -->
	<privacyPop ref="priPop"></privacyPop>
	<!-- #endif -->
	
	<!-- 登录 -->
	<uni-popup ref="popLogin" type="dialog">
		<uni-popup-dialog :before-close="true" type="input" :title="$L('提示')" :content="$L('请登录')" :duration="2000"
			@confirm="confirmLogins" @close="closefn">
		</uni-popup-dialog>
	</uni-popup>
  </view>
</template>

<script>
// import HomeDeco from '@/components/home_deco.vue'
import homeIndex from '@/components/homeIndex.vue'//数据首页
import homeTourist from '@/components/home_tourist.vue'
import homeAssociaton from '@/components/home_association.vue'
import uniPopup from '@/components/uni-popup/uni-popup.vue'
import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue'
import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue'
// #ifdef APP-PLUS
import { checkUpdate } from '@/utils/app-update-check.js'
// #endif
// #ifdef MP
import privacyPop from '@/components/privacy-pop.vue';
// #endif
import { mapState, mapMutations } from 'vuex'
export default {
  data() {
    return {
      deco_data: [], //首页装修数据
      home_is_show_top_cat: true, //是否显示顶部分类，默认显示
      home_page_img: [],
      width: '',
      height: '',
      shareData: {},
      privacyPolicyHtml: '', //隐私政策协议的富文本
      privacyPolicyModel: false,
      triggered: false,
	  address:'',
	  authounum:4,
	  keyinfo:1,
	  rezub:true,
	  webinfo:{},
	  wenshow:false,
    }
  },
  components: {
    // HomeDeco,
	homeIndex,
	homeTourist,
	homeAssociaton,
	uniPopup,
	uniPopupMessage,
	uniPopupDialog,
	// #ifdef MP
	privacyPop,
	// #endif
  },
  computed: {
    ...mapState(['hasLogin', 'userInfo', 'userCenterData'])
  },
  onShow(){
	uni.hideTabBar()  
  },
  onLoad() {
	 this.getshopweb()
	  this.getauthor()
	let _this = this;
	// #ifdef MP
	// 判断用户隐私授权
	if (!getApp().globalData.allow_privacy) {
		if (wx.getPrivacySetting == undefined) {
			//微信低版本不适配该授权方法
			getApp().globalData.allow_privacy = true;
		} else {
			wx.getPrivacySetting({
				success(res) {
					if (res.needAuthorization) {
						_this.$refs.priPop.PrivacyProtocol = {
							needAuthorization: res.needAuthorization,
							privacyContractName: res.privacyContractName
						};
						_this.$refs.priPop.open();
					} else {
						getApp().globalData.allow_privacy = true;
					}
				}
			})
		}
	}
	// #endif
		
    // #ifdef APP-PLUS
    this.getAppInfo(0) //获取线上APP版本信息  参数type 0自动检查  1手动检查（手动检查时，之前取消更新的版本也会提示出来）
    // #endif

    this.loadData()

    // #ifdef MP-WEIXIN
    let url = getApp().globalData.apiUrl.substring(
      0,
      getApp().globalData.apiUrl.length - 1
    )
    this.$sldStatEvent({
      behaviorType: 'pv',
      pageUrl: url + '/pages/index/index',
      referrerPageUrl: ''
    })
    // #endif
	// if (!this.hasLogin) {
	// 	setTimeout(()=>{
	// 		this.$refs.popLogin.open()
	// 	},500)
	// 	return
	// }
	// if (!this.hasLogin) {
	// 	setTimeout(()=>{
	// 		this.$refs.popLogin.open()
	// 	},1000)
	// 	return
	// }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { shareData } = this
    return shareData
  },

  onShareTimeline: function () {
    const { shareData } = this
    return shareData
  },

  onPullDownRefresh() {
	  this.keyinfo+=1
	  this.rezub=false
	  if(this.hasLogin && this.authounum!=4){
	  	this.webinfo={}
	  }
    this.loadData()
	this.getauthor()
	if (!this.hasLogin) {
		setTimeout(()=>{
			this.$refs.popLogin.open()
		},500)
		return
	}
  },
  
  onReachBottom(){
	this.$refs.homeDeco.getMoreData()
  },

  onShow() {
	  this.getauthor()
    if (this.userInfo.access_token) {
      this.$request({
        url: 'v3/helpdesk/front/chat/unReadMsgNum'
      }).then((res) => {
        if (res.state == 200) {
          this.userCenterData.msgNum = res.data
          this.setUserCenterData(this.userCenterData)
        }
      })
    }
	if(this.hasLogin && this.authounum!=4){
		this.webinfo={}
		console.log('清除');
	}
    // this.getCartNum()
	// if (this.hasLogin==false) {
	// 	console.log(this.hasLogin);
	// 	setTimeout(()=>{
	// 		this.$refs.popLogin.open()
	// 	},1000)
	// 	return
	// }
	if(this.hasLogin){
		 this.$refs.popLogin.close()
	}
	
  },
  
  onTabItemTap(e) {
	  this.$refs.homeDeco.restoreTab()
  },

  methods: {
    ...mapMutations(['login', 'setUserCenterData']),
	// 去登录
          
		 // 获取用户身份1、会员, 2、商户，3、平台  4、散户 5、协会
		 getauthor(){
		   this.$request({
		   	url:'v3/seller/front/vendor/roles',
		   	method:"GET",
		   	data:{vendorMobile:this.userCenterData.memberMobile}
		   }).then(res=>{
			   this.authounum=res.msg
			   if(this.authounum==4){
				   this.getshopweb()
			   }
			    this.rezub=true
		   })	 
		 },
		 // 提示登录取消退出‘
		 closefn(){
		
		    this.$refs.popLogin.close()
		    this.getshopweb()
			this.rezub=false
		 	//  uni.navigateBack({delta:1})
		 		},
		 confirmLogins() {
			// 去登录就清除微官网信息
			     this.webinfo={}
		 				// let url = this.$Route.path
		 				// const query = this.$Route.query
		 				this.$refs.popLogin.close()
		 				// uni.setStorageSync('fromurl', {
		 				// 	url,
		 				// 	query
		 				// })
		 		 this.$Router.push('/pages/public/login')
		 	  },
			
				// 获取微官网数据
				getshopweb(){
					// this.$request({
					// 	url:'v3/website/front/member/all/list',
					// 	method:'GET',
					// 	data:{vendorId:'500002'}
					// }).then(res=>{
					// 	this.webinfo=res.data
						
					// })
					let storeId=null
					if(uni.getStorageSync('storeId') && uni.getStorageSync('storeId')!='null'){
						console.log(uni.getStorageSync('storeId'));
						storeId=uni.getStorageSync('storeId')
						this.$request({
							url:'v3/website/front/member/all/list',
							method:'GET',
							data:{vendorId:storeId}
						}).then(res=>{
							this.webinfo=res.data
							
						})
					}else{
						// 东之策640003  500002
						storeId='640003'
						this.$request({
							url:'v3/website/front/member/all/list',
							method:'GET',
							data:{vendorId:storeId}
						}).then(res=>{
							this.webinfo=res.data
							this.wenshow=false
							
						})
					}
						
					},
    // #ifdef APP-PLUS
    //获取线上APP版本信息
    getAppInfo(type) {
      const platform = uni.getSystemInfoSync().platform //本机设备操作系统  android / ios

      //请求获取最新版本
      //这里自行请求API获取版本信息 建议传入操作系统标识，返回本机对应的操作系统最新版本信息，也就是安卓的返回就是安卓的版本信息  ios返回就是ios的版本信息
      let param = {}
      param.url = 'v3/system/front/setting/getSettings'
      param.method = 'GET'
      param.data = {}
      if (platform == 'android') {
        //android
        param.data.names =
          'app_android_hot_edition,' + //热更新版本号
          'app_android_hot_link,' + //热更新地址
          'app_android_hot_tip,' + //更新提示内容
          'app_android_package_edition,' + //整包升级版本号
          'app_android_package_link' //整包升级地址
      } else if (platform == 'ios') {
        //ios
        param.data.names =
          'app_ios_hot_edition,' + //热更新版本号
          'app_ios_hot_link,' + //热更新地址
          'app_ios_hot_tip,' + //更新提示内容
          'app_ios_package_edition,' + //整包升级版本号
          'app_ios_package_link' //整包升级地址
      } else {
        return false
      }

      this.$request(param).then((res) => {
        if (res.state == 200) {
          let update_info = {
            version: res.data[3], //整包版本
            versionCode: res.data[0], //热更新版本号
            now_url: '', //更新链接
            version_url: res.data[4], //整包下载地址
            versionCode_url: res.data[1], //热更新下载地址
            silent: 0, //是否是静默更新
            force: 1, //是否是强制更新
            net_check: 1, //非WIfi是否提示
            note: res.data[2] //更新内容
          }
          checkUpdate(update_info, type).then((res) => {
            if (res.data) {
              plus.nativeUI.toast(res.data.msg)
            }
          }) ///检查更新 线上版本号与本地版本号做对比,如果需要更新 根据静默、强制、wifi等信息执行静默更新或跳转到升级页面
        }
      })
    },
    // #endif

    needLogin() {
      this.$refs.loginPop.openLogin()
    },
    confirmLogin() {
      this.$refs.loginPop.close()
    },

    async loadData() {
      uni.showLoading({
        title: this.$L('加载中....')
      })
	 
      // #ifdef H5
      this.client = 'h5'
      // #endif

      // #ifdef APP-PLUS
      switch (uni.getSystemInfoSync().platform) {
        case 'android':
          this.client = 'android'
          break
        case 'ios':
          this.client = 'ios'
          break
        default:
          break
      }
      // #endif

      // #ifdef MP
      this.client = 'weixinXcx'
      // #endif
      let param = {}
      param.url = 'v3/system/front/deco/index?os=' + this.client
      param.method = 'GET'
      this.$request(param).then((res) => {
        uni.stopPullDownRefresh()
        if (res.state == 200) {
          if (JSON.stringify(res.data) == '{}') {
            this.deco_data = null
            uni.hideLoading()
            return
          }
          if (res.data.data != '') {
            this.deco_data = JSON.parse(res.data.data)
          } else {
            this.deco_data = null
          }

          uni.stopPullDownRefresh()
          // #ifdef MP
          this.shareData = {
            title: res.data.siteName,
            path: '/pages/index/index',
            imageUrl: res.data.xcxImage
          }
          // #endif
          if (res.data.showTip != null) {
            this.home_page_img = JSON.parse(res.data.showTip)
            const { windowWidth, windowHeight } = uni.getSystemInfoSync()
            this.width = this.home_page_img[0].width || windowWidth * 0.75 * 1.8
            this.height =
              this.home_page_img[0].height || windowHeight * 0.56 * 1.8
          } else {
            this.home_page_img = []
          }

          if (
            this.deco_data &&
            this.deco_data.length != undefined &&
            this.deco_data.length > 0
          ) {
            this.home_is_show_top_cat =
              this.deco_data[0].type == 'top_cat_nav' ? true : false
          }
          uni.hideLoading()
        }
      })
    },
    //阻止模态框下页面滚动
    moveHandle() {},
    //获取购物车数据
    getCartNum() {
      if (this.hasLogin) {
        let param = {}
        param.url = 'v3/business/front/cart/cartNum'
        param.method = 'GET'
        param.data = {}
        // param.data.key = this.userInfo.access_token;
        this.$request(param)
          .then((res) => {
            if (res.state == 200) {
              if (res.data > 0) {
                uni.setTabBarBadge({
                  index: 3,
                  text: res.data.toString()
                })
              } else {
                uni.hideTabBarRedDot({
                  index: 3
                })
              }
            } else {
              this.$api.msg(res.msg)
            }
          })
          .catch((e) => {
            //异常处理
          })
      } else {
        this.getNoLoginCartNum()
      }
    },
    //获取未登录，购物车数量
    getNoLoginCartNum() {
      let cartNum = 0
      let cart_list = uni.getStorageSync('cart_list')
      if (cart_list && cart_list.storeCartGroupList) {
        cart_list.storeCartGroupList.map((item) => {
          item.promotionCartGroupList.map((item1) => {
            item1.cartList.map((item2) => {
              cartNum++
            })
          })
        })
      }
      if (cartNum > 0) {
        uni.setTabBarBadge({
          index: 3,
          text: cartNum.toString()
        })
      } else {
        uni.hideTabBarRedDot({
          index: 3
        })
      }
    }
  }
}
</script>

<style lang="scss">
.fixed_top_status_bar {
  position: fixed;
  /* #ifdef APP-PLUS */
  height: var(--status-bar-height);
  /* #endif */
  /* #ifndef APP-PLUS */
  height: 0;
  /* #endif */
  top: 0;
  left: 0;
  right: 0;
  z-index: 99;
  background: #fff;
}

page {
  background: #f5f5f5;
}

.privacy_policy_model {
  width: 750rpx;
  height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999999;
  overflow: hidden;

  .privacy_model {
    width: 625rpx;
    height: 70vh;
    background: #ffffff;
    border-radius: 15rpx;
    margin: 0 auto;
    margin-top: 17vh;
    padding-bottom: 24rpx;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .privacy_model_title {
      height: 104rpx;
      font-size: 34rpx;
      font-family: Source Han Sans CN;
      font-weight: bold;
      color: #000000;
      line-height: 104rpx;
      text-align: center;
      border-bottom: 1rpx solid #f2f2f2;
    }

    .privacy_policy {
      width: 625rpx;
      height: 50vh;
      padding: 0 43rpx;
      box-sizing: border-box;
    }

    .privacy_model_confirm {
      width: 542rpx;
      height: 72rpx;
      background: linear-gradient(90deg, var(--color_main) 0%, #ff7918 100%);
      border-radius: 36rpx;
      font-size: 28rpx;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #ffffff;
      line-height: 72rpx;
      text-align: center;
      margin: 20rpx auto;
    }

    .privacy_model_cancel {
      font-size: 28rpx;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #ff991e;
      text-align: center;
      line-height: 28rpx;
    }
  }
}
</style>
